﻿package core.image
{
    import core.enum.*;
    import flash.display.*;
    import flash.geom.*;

    public class Document extends Sprite
    {
        public var transparent:Boolean;
        public var anr:int = 0;
        public var layercount:int = 0;
        public var dw:int = 0;
        public var dh:int = 0;

        public function Document(param1:String, param2:int, param3:int)
        {
            this.dw = param2;
            this.dh = param3;
            this.name = param1;
            return;
        }// end function

        public function GetName() : String
        {
            if (name.indexOf(".") != -1)
            {
                return name.substring(0, name.lastIndexOf("."));
            }
            return name;
        }// end function

        public function GetType() : String
        {
            if (name.indexOf(".") != -1)
            {
                return name.substr((name.lastIndexOf(".") + 1));
            }
            return "jpg";
        }// end function

        public function AddBitmapLayer(core.image:Document/GetType:BitmapData, core.image:Document/GetType:int = -1, core.image:Document/GetType:Point = null) : void
        {
            if (this.layercount == 0)
            {
                this.transparent = Misc.IsTransparent(core.image:Document/GetType);
            }
            var _loc_4:* = new Layer(LayerType.Bitmap, this.transparent || this.layercount > 0 ? (U.RM("rm_layer") + " " + this.layercount) : (U.RM("rm_background")), core.image:Document/GetType, true, 1);
            _loc_4.AddBitmap(core.image:Document/GetType);
            this.AddLayer(_loc_4, core.image:Document/GetType);
            return;
        }// end function

        public function AddTextLayer(core.image:Document/GetType:int = -1, core.image:Document/GetType:Point = null, core.image:Document/GetType:String = "", core.image:Document/GetType:int = 10, core.image:Document/GetType:uint = 0, core.image:Document/GetType:Boolean = false, core.image:Document/GetType:Boolean = false) : void
        {
            var _loc_8:* = new Layer(LayerType.Text, U.RM("rm_layer") + " " + this.layercount, core.image:Document/GetType, true, 1);
            _loc_8.AddText(core.image:Document/GetType, core.image:Document/GetType, core.image:Document/GetType, core.image:Document/GetType, core.image:Document/GetType);
            this.AddLayer(_loc_8, core.image:Document/GetType);
            return;
        }// end function

        public function AddLayer(core.image:Document/GetType:Layer, core.image:Document/GetType:int = -1) : void
        {
            if (core.image:Document/GetType == -1)
            {
                this.addChild(core.image:Document/GetType);
                this.anr = this.numChildren - 1;
            }
            else
            {
                this.addChildAt(core.image:Document/GetType, core.image:Document/GetType);
                this.anr = core.image:Document/GetType;
            }
            var _loc_3:String = this;
            var _loc_4:* = this.layercount + 1;
            _loc_3.layercount = _loc_4;
            return;
        }// end function

        public function RemoveLayer(core.image:Document/GetType:int = -1) : void
        {
            if (core.image:Document/GetType == -1 || core.image:Document/GetType == this.anr)
            {
                core.image:Document/GetType = this.anr;
                var _loc_2:String = this;
                var _loc_3:* = this.anr - 1;
                _loc_2.anr = _loc_3;
            }
            removeChildAt(core.image:Document/GetType);
            if (this.anr >= this.numChildren)
            {
                this.anr = this.numChildren - 1;
            }
            else if (this.anr == -1)
            {
                this.anr = 0;
            }
            return;
        }// end function

        public function MergeLayers(core.image:Document/GetType:int, core.image:Document/GetType:int) : void
        {
            var _loc_3:* = this.GetLayer(core.image:Document/GetType);
            var _loc_4:* = this.GetLayer(core.image:Document/GetType);
            if (_loc_4.type != LayerType.Bitmap)
            {
                this.RasterizeLayer(core.image:Document/GetType);
            }
            if (_loc_3.type != LayerType.Bitmap || _loc_3.filters != null && _loc_3.filters.length > 0)
            {
                this.RasterizeLayer(core.image:Document/GetType, true);
            }
            _loc_4.PresetBitmapEditMode(_loc_3.bitmap.bitmapData, new Point(_loc_3.x, _loc_3.y), _loc_3.alpha, _loc_3.blendMode);
            this.RemoveLayer(core.image:Document/GetType);
            return;
        }// end function

        public function LayerOrder(core.image:Document/GetType:int, core.image:Document/GetType:int) : void
        {
            this.setChildIndex(this.getChildAt(core.image:Document/GetType), core.image:Document/GetType);
            this.anr = core.image:Document/GetType;
            return;
        }// end function

        public function Clear() : void
        {
            while (this.numChildren > 0)
            {
                
                this.removeChildAt(0);
            }
            return;
        }// end function

        public function LayerVisibility(core.image:Document/GetType:Boolean, core.image:Document/GetType:int) : void
        {
            getChildAt(core.image:Document/GetType).visible = core.image:Document/GetType;
            return;
        }// end function

        public function LayerOpacity(core.image:Document/GetType:Number, core.image:Document/GetType:int = -1) : void
        {
            getChildAt(core.image:Document/GetType != -1 ? (core.image:Document/GetType) : (this.anr)).alpha = core.image:Document/GetType;
            return;
        }// end function

        public function MoveLayer(core.image:Document/GetType:Point) : void
        {
            getChildAt(this.anr).x = getChildAt(this.anr).x + core.image:Document/GetType.x;
            getChildAt(this.anr).y = getChildAt(this.anr).y + core.image:Document/GetType.y;
            return;
        }// end function

        public function GetLayer(indexOf:int = -1) : Layer
        {
            return getChildAt(indexOf != -1 ? (indexOf) : (this.anr)) as Layer;
        }// end function

        public function RasterizeLayer(core.image:Document/GetType:int = -1, core.image:Document/GetType:Boolean = false) : void
        {
            var _loc_3:BitmapData = null;
            var _loc_4:* = this.GetLayer(core.image:Document/GetType);
            if (!core.image:Document/GetType)
            {
                _loc_3 = new BitmapData(_loc_4.width, _loc_4.height, true, 16777215);
                _loc_3.draw(_loc_4.holder);
            }
            else
            {
                _loc_3 = new BitmapData(this.dw, this.dh, true, 16777215);
                _loc_3.draw(_loc_4);
            }
            _loc_4.RemoveObject();
            _loc_4.AddBitmap(_loc_3);
            return;
        }// end function

        public function Crop(core.image:Document/GetType:Rectangle) : void
        {
            var _loc_2:Layer = null;
            var _loc_3:* = new Reform();
            this.dw = core.image:Document/GetType.width;
            this.dh = core.image:Document/GetType.height;
            var _loc_4:* = 0;
            while (_loc_4 < numChildren)
            {
                
                _loc_2 = getChildAt(_loc_4) as Layer;
                switch(_loc_2.type)
                {
                    case LayerType.Bitmap:
                    {
                        _loc_3.Crop(_loc_2, core.image:Document/GetType);
                        break;
                    }
                    case LayerType.Text:
                    {
                        _loc_2.x = _loc_2.x - core.image:Document/GetType.x;
                        _loc_2.y = _loc_2.y - core.image:Document/GetType.y;
                        break;
                    }
                    case LayerType.Shape:
                    {
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                _loc_4 = _loc_4 + 1;
            }
            return;
        }// end function

        public function Newsize(core.image:Document/GetType:int, core.image:Document/GetType:int, core.image:Document/GetType:int) : void
        {
            var _loc_4:Layer = null;
            var _loc_5:* = new Point(0, 0);
            var _loc_6:* = new Reform();
            switch(core.image:Document/GetType)
            {
                case 1:
                {
                    _loc_5.x = 0;
                    _loc_5.y = 0;
                    break;
                }
                case 2:
                {
                    _loc_5.y = 0;
                    _loc_5.x = Math.round((core.image:Document/GetType - this.dw) / 2);
                    break;
                }
                case 3:
                {
                    _loc_5.y = 0;
                    _loc_5.x = core.image:Document/GetType - this.dw;
                    break;
                }
                case 4:
                {
                    _loc_5.x = 0;
                    _loc_5.y = Math.round((core.image:Document/GetType - this.dh) / 2);
                    break;
                }
                case 5:
                {
                    _loc_5.x = Math.round((core.image:Document/GetType - this.dw) / 2);
                    _loc_5.y = Math.round((core.image:Document/GetType - this.dh) / 2);
                    break;
                }
                case 6:
                {
                    _loc_5.x = core.image:Document/GetType - this.dw;
                    _loc_5.y = Math.round((core.image:Document/GetType - this.dh) / 2);
                    break;
                }
                case 7:
                {
                    _loc_5.x = 0;
                    _loc_5.y = core.image:Document/GetType - this.dh;
                    break;
                }
                case 8:
                {
                    _loc_5.y = core.image:Document/GetType - this.dh;
                    _loc_5.x = Math.round((core.image:Document/GetType - this.dw) / 2);
                    break;
                }
                case 9:
                {
                    _loc_5.x = core.image:Document/GetType - this.dw;
                    _loc_5.y = core.image:Document/GetType - this.dh;
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_7:* = 0;
            while (_loc_7 < numChildren)
            {
                
                _loc_4 = getChildAt(_loc_7) as Layer;
                if (_loc_4.type == LayerType.Bitmap && !this.transparent && _loc_7 == 0)
                {
                    if (_loc_4.hasBitmap)
                    {
                        _loc_4.bitmap.bitmapData = _loc_6.Newsize(_loc_4.bitmap.bitmapData, core.image:Document/GetType, core.image:Document/GetType, _loc_5, _loc_7 > 0 || this.transparent ? (16777215) : (4294967295));
                    }
                }
                else
                {
                    _loc_4.x = _loc_4.x + _loc_5.x;
                    _loc_4.y = _loc_4.y + _loc_5.y;
                }
                _loc_7 = _loc_7 + 1;
            }
            this.dw = core.image:Document/GetType;
            this.dh = core.image:Document/GetType;
            return;
        }// end function

        public function Resize(core.image:Document/GetType:int, core.image:Document/GetType:int) : void
        {
            var _loc_3:Layer = null;
            var _loc_4:* = new Reform();
            var _loc_5:* = core.image:Document/GetType / this.dw;
            var _loc_6:* = core.image:Document/GetType / this.dh;
            var _loc_7:* = 0;
            while (_loc_7 < numChildren)
            {
                
                _loc_3 = getChildAt(_loc_7) as Layer;
                if (_loc_3.type == LayerType.Bitmap && _loc_3.hasBitmap)
                {
                    _loc_3.bitmap.bitmapData = _loc_4.Resize(_loc_3.bitmap.bitmapData, _loc_5, _loc_6);
                    if (_loc_3.hasMask && _loc_3.masking.bitmapData != null)
                    {
                        _loc_3.masking.bitmapData = _loc_4.Resize(_loc_3.masking.bitmapData, _loc_5, _loc_6);
                    }
                }
                _loc_3.x = Math.round(_loc_3.x * _loc_5);
                _loc_3.y = Math.round(_loc_3.y * _loc_6);
                _loc_7 = _loc_7 + 1;
            }
            this.dw = core.image:Document/GetType;
            this.dh = core.image:Document/GetType;
            return;
        }// end function

        public function Flip(core.image:Document/GetType:FlipType, core.image:Document/GetType:Boolean = false) : void
        {
            var _loc_3:Layer = null;
            var _loc_4:* = undefined;
            var _loc_5:* = new Reform();
            if (core.image:Document/GetType)
            {
                _loc_4 = 0;
                while (_loc_4 < numChildren)
                {
                    
                    _loc_3 = getChildAt(_loc_4) as Layer;
                    if (_loc_3.type == LayerType.Bitmap && _loc_3.hasBitmap)
                    {
                        _loc_5.Flip(_loc_3.bitmap.bitmapData, core.image:Document/GetType);
                        if (_loc_3.hasMask && _loc_3.masking.bitmapData != null)
                        {
                            _loc_5.Flip(_loc_3.masking.bitmapData, core.image:Document/GetType);
                        }
                    }
                    switch(core.image:Document/GetType)
                    {
                        case FlipType.Vertical:
                        {
                            _loc_3.y = Math.round(this.dh - (_loc_3.y + _loc_3.height));
                            break;
                        }
                        case FlipType.Horizontal:
                        {
                            _loc_3.x = Math.round(this.dw - (_loc_3.x + _loc_3.width));
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    _loc_4 = _loc_4 + 1;
                }
            }
            else
            {
                _loc_3 = getChildAt(this.anr) as Layer;
                if (_loc_3.type == LayerType.Bitmap && _loc_3.hasBitmap)
                {
                    _loc_5.Flip(_loc_3.bitmap.bitmapData, core.image:Document/GetType);
                    if (_loc_3.hasMask && _loc_3.masking.bitmapData != null)
                    {
                        _loc_5.Flip(_loc_3.masking.bitmapData, core.image:Document/GetType);
                    }
                }
            }
            return;
        }// end function

        public function Rotate(core.image:Document/GetType:int, core.image:Document/GetType:Boolean = false) : void
        {
            var _loc_3:Layer = null;
            var _loc_4:BitmapData = null;
            var _loc_5:* = undefined;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:* = new Reform();
            if (core.image:Document/GetType)
            {
                _loc_5 = 0;
                while (_loc_5 < numChildren)
                {
                    
                    _loc_3 = getChildAt(_loc_5) as Layer;
                    _loc_6 = _loc_3.x;
                    _loc_7 = _loc_3.y;
                    switch(core.image:Document/GetType)
                    {
                        case 180:
                        {
                            _loc_3.y = Math.round(this.dh - _loc_7 - _loc_3.height);
                            _loc_3.x = Math.round(this.dw - _loc_6 - _loc_3.width);
                            break;
                        }
                        case 90:
                        {
                            _loc_3.x = Math.round(this.dh - _loc_7 - _loc_3.height);
                            _loc_3.y = Math.round(_loc_6);
                            break;
                        }
                        case 270:
                        {
                            _loc_3.y = Math.round(this.dw - _loc_6 - _loc_3.width);
                            _loc_3.x = Math.round(_loc_7);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    if (_loc_3.type == LayerType.Bitmap && _loc_3.hasBitmap)
                    {
                        _loc_3.bitmap.bitmapData = _loc_10.Rotate(_loc_3.bitmap.bitmapData, core.image:Document/GetType);
                        if (_loc_3.hasMask && _loc_3.masking.bitmapData != null)
                        {
                            _loc_3.masking.bitmapData = _loc_10.Rotate(_loc_3.masking.bitmapData, core.image:Document/GetType);
                        }
                    }
                    _loc_5 = _loc_5 + 1;
                }
                if (core.image:Document/GetType != 180)
                {
                    _loc_8 = this.dh;
                    _loc_9 = this.dw;
                    this.dw = _loc_8;
                    this.dh = _loc_9;
                }
            }
            else
            {
                _loc_3 = getChildAt(this.anr) as Layer;
                if (_loc_3.type == LayerType.Bitmap && _loc_3.hasBitmap)
                {
                    _loc_3.bitmap.bitmapData = _loc_10.Rotate(_loc_3.bitmap.bitmapData, core.image:Document/GetType);
                    if (_loc_3.hasMask && _loc_3.masking.bitmapData != null)
                    {
                        _loc_3.masking.bitmapData = _loc_10.Rotate(_loc_3.masking.bitmapData, core.image:Document/GetType);
                    }
                }
            }
            return;
        }// end function

    }
}
